﻿Imports OMANE.Common.UI
Imports System.Collections.ObjectModel
Imports System.Windows
Imports Infragistics.Windows.Chart
Imports System.Windows.Controls
Imports System.ComponentModel
Imports PTIC_GestionBDD
Imports System.Collections.Specialized
Imports Microsoft.Practices.Prism.Commands

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class VMSectionBase
    Inherits ViewModelBase
    Implements IViewModelTabSection

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Title As String Implements Common.UI.IViewModelTabSection.Title

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class VMExpressionEditor
    Inherits ViewModelBase

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class VMSqlQuery
    Inherits ViewModelBase

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Sub New()
        AddHandler Lines.CollectionChanged, AddressOf LinesCollectionChanged

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Lines As New ObservableCollection(Of VMSqlQueryLine)

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LinesCollectionChanged(ByVal sender As Object, ByVal e As Specialized.NotifyCollectionChangedEventArgs)
        Select Case e.Action
            Case Specialized.NotifyCollectionChangedAction.Add
                CType(e.NewItems(0), VMSqlQueryLine)._Tables = New String() { _
                    "None", _
                    "Locus"}
        End Select
    End Sub

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class VMSqlQueryLine
    Inherits ViewModelBase

    Private _Name As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Name")
            _Name = value
            OnPropertyChanged("Name")
        End Set
    End Property

    Private _Expression As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Expression As String
        Get
            Return _Expression
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Expression")
            _Expression = value
            OnPropertyChanged("Expression")
        End Set
    End Property

    Protected Friend _Tables As IEnumerable
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property Tables As IEnumerable
        Get
            Return _Tables
        End Get
    End Property

    Protected Friend _Columns As IEnumerable
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property Columns As IEnumerable
        Get
            Return _Columns
        End Get
    End Property

    Private _Table As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Table As String
        Get
            Return _Table
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Table")
            _Table = value
            OnPropertyChanged("Table")
        End Set
    End Property

    Private _Filtre As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Filtre As String
        Get
            Return _Filtre
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Filtre")
            _Filtre = value
            OnPropertyChanged("Filtre")
        End Set
    End Property

    Private _Tri As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Tri As String
        Get
            Return _Tri
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Tri")
            _Tri = value
            OnPropertyChanged("Tri")
        End Set
    End Property


    Private _Show As Boolean
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Show As Boolean
        Get
            Return _Show
        End Get
        Set(ByVal value As Boolean)
            OnPropertyChanging("Show")
            _Show = value
            OnPropertyChanged("Show")
        End Set
    End Property

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class VMChartZone
    Inherits ViewModelBase


    Dim _data As Object
    Public Property Banque As String = ""

    Sub New()

    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    <Xml.Serialization.XmlIgnore()> _
    Public Property Series As New ObservableCollection(Of VMChartZoneSerie)

    Public Property SerializableSerie As VMChartZoneSerie()
        Get
            Return Series.ToArray()
        End Get
        Set(ByVal value As VMChartZoneSerie())
            Series = New ObservableCollection(Of VMChartZoneSerie)(value)

        End Set
    End Property

    Private _AddSerieCommand As New DelegateCommand(Of Object)(AddressOf AddSerieExecute, AddressOf AddSerieCanExecute)
    Public ReadOnly Property AddSerieCommand As DelegateCommand(Of Object)
        Get
            Return _AddSerieCommand
        End Get
    End Property
    Private Sub AddSerieExecute(ByVal parameter As Object)
        Series.Add(New VMChartZoneSerie(Me, InputBox("Nom de la Série")))
        If Series.Count = 1 Then
            ActiveSerie = Series(0)
        End If
        OnPropertyChanged("HaveSerie")
    End Sub
    Private Function AddSerieCanExecute(ByVal parameter As Object) As Boolean
        Return True
    End Function

    Private _DeleteSerieCommand As New DelegateCommand(Of Object)(AddressOf DeleteSerieExecute, AddressOf DeleteSerieCanExecute)
    Public ReadOnly Property DeleteSerieCommand As DelegateCommand(Of Object)
        Get
            Return _DeleteSerieCommand
        End Get
    End Property
    Private Sub DeleteSerieExecute(ByVal parameter As Object)
        If ActiveSerie Is Nothing Then Return
        Series.Remove(ActiveSerie)
        ActiveSerie = Nothing
        OnPropertyChanged("HaveSerie")
    End Sub
    Private Function DeleteSerieCanExecute(ByVal parameter As Object) As Boolean
        Return True
    End Function

    Private _ActiveSerie As VMChartZoneSerie
    Public Property ActiveSerie As VMChartZoneSerie
        Get
            Return _ActiveSerie
        End Get
        Set(ByVal value As VMChartZoneSerie)
            OnPropertyChanging("ActiveSerie")
            _ActiveSerie = value
            OnPropertyChanged("ActiveSerie")
            OnPropertyChanged("HaveSerie")
            OnPropertyChanged("ValueY")
        End Set
    End Property

    Public ReadOnly Property HaveSerie As Boolean
        Get
            Return Series.Count > 0
        End Get
    End Property


    Private _Label As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Label As String
        Get
            Return _Label
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Label")
            _Label = value
            OnPropertyChanged("Label")
            RaiseEvent SerieDataChange(Me, EventArgs.Empty)
        End Set
    End Property

    Private _ValueX As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property ValueX As String
        Get
            Return _ValueX
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("ValueX")
            _ValueX = value
            OnPropertyChanged("ValueX")
            RaiseEvent SerieDataChange(Me, EventArgs.Empty)
        End Set
    End Property

    Private _ValueY As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property ValueY As String
        Get
            If HaveSerie Then
                If ActiveSerie IsNot Nothing Then
                    Return ActiveSerie.ValueY
                End If
            End If
            Return _ValueY
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("ValueY")
            If HaveSerie Then
                If ActiveSerie IsNot Nothing Then
                    ActiveSerie.ValueY = value
                Else
                    _ValueY = value
                End If
            Else
                _ValueY = value
            End If
            OnPropertyChanged("ValueY")
            RaiseEvent SerieDataChange(Me, EventArgs.Empty)
        End Set
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable ReadOnly Property Data As IEnumerable
        Get
            Return _data
        End Get
    End Property

    Public Event SerieDataChange As EventHandler

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SerieDataChangeHandled(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SerieDataChange
        If ModuleIndicateurs.MuteAllQuery Then Return

        If String.IsNullOrEmpty(ValueY) Then Return

        If ValueY.StartsWith("::") Then
            _data = Indicateus.Instance.GetIndicateur(ValueY.Replace("::", "").Trim(), Banque)
            If ActiveSerie IsNot Nothing Then
                ActiveSerie.SetData(_data)
            End If
            OnPropertyChanged("Data")
            Return
        End If

        If Not String.IsNullOrEmpty(ValueX) Then

            Dim tbl = ModuleIndicateurs.BddInstance.ListeTraitements.TraitTbleEncours
            Dim nomtrait = "traitement_" & Guid.NewGuid().ToString("N")
            Dim req = New RequeteBDD(nomtrait, _
                            New Cla_NomFichier(PTIC_Util.csteEmplacementCalculsInt & nomtrait & ".sas7bdat"), _
                            PTIC_Util.csteLibSASCalculsInt, _
                            ModuleIndicateurs.BddInstance.Connection.Connection)
            '   AddHandler req.EtatCalculDataTableAChange, AddressOf ReqEtatCalculDataTableAChange
            req.isDISTINCT = False
            req.addTraitementConstitutif(tbl)
            req.addSELECT(tbl.nom, ValueX.Trim())

            If ValueY.Trim().StartsWith("=") Then
                If String.IsNullOrEmpty(Label) Then Return
                Dim vars = Label.Trim().Split(New Char() {";"}, StringSplitOptions.RemoveEmptyEntries)
                Dim tvars = Enumerable.Repeat(Of String)(tbl.nom, vars.Length).ToArray()
                req.addSELECTcalculee( _
                              tvars, _
                              vars, _
                              ValueY.Trim().Remove(0, 1).Trim(), _
                              "X", _
                              "Calculer")

                req.addGROUPBY(tbl.nom, ValueX.Trim())
            Else
                req.addSELECTcalculee( _
                               New String() {tbl.nom}, _
                               New String() {ValueX.Trim()}, _
                               "count({1})", _
                               "X", _
                               "Calculer")

                req.addGROUPBY(tbl.nom, ValueX.Trim())
            End If




            ' req.addSELECTcalculee(
            ' req.addSELECT(tbl.nom, ValueY.Trim())
            req.Calculer_DataTable()

            'Dim t As DataTable = req.DataTableResultat
            'Dim s As String = "t"
            'If req.DataTableResultat Is Nothing Then Return

            _data = (From elt As DataRowView In req.DataTableResultat.DefaultView
                         Select X = elt("X"), Lbl = elt(0)).ToArray()

            If ActiveSerie IsNot Nothing Then
                ActiveSerie.SetData(_data)
            End If
            OnPropertyChanged("Data")
            Return
        End If

        If Not String.IsNullOrEmpty(Label) Then
            Dim tbl = ModuleIndicateurs.BddInstance.ListeTraitements.TraitTbleEncours
            Dim nomtrait = "traitement_" & Guid.NewGuid().ToString("N")
            Dim req = New RequeteBDD(nomtrait, _
                            New Cla_NomFichier(PTIC_Util.csteEmplacementCalculsInt & nomtrait & ".sas7bdat"), _
                            PTIC_Util.csteLibSASCalculsInt, _
                            ModuleIndicateurs.BddInstance.Connection.Connection)
            '   AddHandler req.EtatCalculDataTableAChange, AddressOf ReqEtatCalculDataTableAChange
            req.addTraitementConstitutif(tbl)
            req.addSELECT(tbl.nom, Label)
            req.addSELECTsum(tbl.nom, ValueY.Trim(), "X", "Calculer", "Double")
            req.addGROUPBY(tbl.nom, Label)
            ' req.addSELECTcalculee(
            ' req.addSELECT(tbl.nom, ValueY.Trim())
            req.Calculer_DataTable()

            'Dim t As DataTable = req.DataTableResultat
            'Dim s As String = "t"

            _data = (From elt As DataRowView In req.DataTableResultat.DefaultView
                         Select X = elt("X"), Lbl = elt(0)).ToArray()



        End If

        If ActiveSerie IsNot Nothing Then
            ActiveSerie.SetData(_data)
            Series(0).SetData(_data)
        End If
        OnPropertyChanged("Data")
        Return

        If ValueY.Trim().StartsWith("=") Then
            Dim c As New DataColumn()
            c.Expression = ValueY.Trim().Remove(0, 1)
            TestData.data.Columns.Add(c)

            If String.IsNullOrEmpty(ValueX) Then
                _data = (From elt In TestData.DataIndicator
                                                  Select X = elt.Item(c.ColumnName), Lbl = "").ToArray()
            Else
                _data = (From elt In TestData.DataIndicator
                                      Select X = elt.Item(c.ColumnName), Lbl = elt.Item(ValueX.Trim())).ToArray()
            End If
        Else
            If String.IsNullOrEmpty(ValueX) Then
                _data = (From elt In TestData.DataIndicator
                                        Select X = elt.Item(ValueY), Lbl = "").ToArray()
            Else
                _data = (From elt In TestData.DataIndicator
                            Select X = elt.Item(ValueY), Lbl = elt.Item(ValueX.Trim())).ToArray()
            End If
        End If

        If ActiveSerie IsNot Nothing Then
            ActiveSerie.SetData(_data)
        End If
        OnPropertyChanged("Data")
    End Sub

#Region "Axe Y"

    Private _PrimaryYAxis As New Axe() With {.Visible = True}
    <EditableProperty(Group:="Axe Y Primaire", BrowseChildren:=True)> _
    Public Property PrimaryYAxis As Axe
        Get
            Return _PrimaryYAxis
        End Get
        Set(ByVal value As Axe)
            OnPropertyChanging("PrimaryYAxis")
            _PrimaryYAxis = value
            OnPropertyChanged("PrimaryYAxis")
        End Set
    End Property

    Private _SecondaryYAxis As New Axe()
    <EditableProperty(Group:="Axe Y Secondaire", BrowseChildren:=True)> _
    Public Property SecondaryYAxis As Axe
        Get
            Return _SecondaryYAxis
        End Get
        Set(ByVal value As Axe)
            OnPropertyChanging("SecondaryYAxis")
            _SecondaryYAxis = value
            OnPropertyChanged("SecondaryYAxis")
        End Set
    End Property

    Private _XAxis As New Axe() With {.Visible = True}
    <EditableProperty(Group:="Axe X", BrowseChildren:=True)> _
    Public Property XAxis As Axe
        Get
            Return _XAxis
        End Get
        Set(ByVal value As Axe)
            OnPropertyChanging("XAxis")
            _XAxis = value
            OnPropertyChanged("XAxis")
        End Set
    End Property

#End Region

#Region "Légende"

    Private _ShowLegend As Boolean = False
    <EditableProperty(Group:="Titre")> _
    Public Property ShowLegend As Boolean
        Get
            Return _ShowLegend
        End Get
        Set(ByVal value As Boolean)
            OnPropertyChanging("ShowLegend")
            _ShowLegend = value
            OnPropertyChanged("ShowLegend")
        End Set
    End Property


#End Region

#Region "Title"

    Private _Police As Police = Indicateurs.Police.Default
    <EditableProperty(Group:="Titre")> _
    Public Property Police As Police
        Get
            Return _Police
        End Get
        Set(ByVal value As Police)
            OnPropertyChanging("Police")
            _Police = value
            OnPropertyChanged("Police")
        End Set
    End Property

    Private _TitleColor As Media.Color
    <EditableProperty(Group:="Titre")> _
    Public Property TitleColor As Media.Color
        Get
            Return _TitleColor
        End Get
        Set(ByVal value As Media.Color)
            OnPropertyChanging("TitleColor")
            _TitleColor = value
            OnPropertyChanged("TitleColor")
        End Set
    End Property

    Private _Title As String
    <EditableProperty(Group:="Titre")> _
    Public Property Title As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Title")
            _Title = value
            OnPropertyChanged("Title")
        End Set
    End Property

    Private _ShowTitle As Boolean = False
    <EditableProperty(Group:="Titre")> _
    Public Property ShowTitle As Boolean
        Get
            Return _ShowTitle
        End Get
        Set(ByVal value As Boolean)
            OnPropertyChanging("ShowTitle")
            _ShowTitle = value
            ShowLegend = value
            OnPropertyChanged("ShowTitle")
        End Set
    End Property

#End Region

    Private Sub ReqEtatCalculDataTableAChange(ByVal sender As Object, ByVal e As EventArgs)
        Dim r = TryCast(sender, RequeteBDD)
        If r Is Nothing Then Return

        If r.EtatCalcul_DataTable = PTIC_Util.ValueEtatCalcul.A_JOUR Then
            Dim o = 1
            Dim t = r.DataTableResultat

        End If
    End Sub

    Public Sub ReCalculate()
        SerieDataChangeHandled(Nothing, Nothing)
    End Sub
End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<Serializable()>
Public Class VMChartZoneSerie
    Inherits ViewModelBase
    'Implements IEnumerable

    Dim owner As VMChartZone
    Dim name As String
    <Xml.Serialization.XmlIgnore()>
    Friend data As IEnumerable


    Sub New(ByVal owner As VMChartZone, ByVal name As String)
        Me.owner = owner
        Me.name = name
    End Sub

    Sub New()

    End Sub

    Public ReadOnly Property ShowName As String
        Get
            If owner Is Nothing Then Return Nothing
            Dim n = If(String.IsNullOrEmpty(name), "(unnamed)", name.Trim().ToLower())
            If Not owner.Series.Contains(Me) Then Return Nothing
            Return 1 + owner.Series.IndexOf(Me) & ": " & n
        End Get
    End Property

    Private _Label As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Label As String
        Get
            Return _Label
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Label")
            _Label = value
            OnPropertyChanged("Label")
        End Set
    End Property

    Private _ValueY As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property ValueY As String
        Get
            Return _ValueY
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("ValueY")
            _ValueY = value
            OnPropertyChanged("ValueY")
        End Set
    End Property

    Private _ValueX As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property ValueX As String
        Get
            Return _ValueX
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("ValueX")
            _ValueX = value
            OnPropertyChanged("ValueX")
        End Set
    End Property

    Private _UseSecondaryAxis As Boolean = False
    Public Property UseSecondaryAxis As Boolean
        Get
            Return _UseSecondaryAxis
        End Get
        Set(ByVal value As Boolean)
            OnPropertyChanging("UseSecondaryAxis")
            _UseSecondaryAxis = value
            OnPropertyChanged("UseSecondaryAxis")
        End Set
    End Property

    Public ReadOnly Property YAXis As String
        Get
            Return If(UseSecondaryAxis, "Secondary", "Primary")
        End Get
    End Property

    Public Event DataChanged As EventHandler

    Public Sub SetData(ByVal data As IEnumerable)
        Me.data = data
        RaiseEvent DataChanged(Me, EventArgs.Empty)
    End Sub

    'Private Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
    '    If data Is Nothing Then Return Nothing
    '    Return data.GetEnumerator()
    'End Function
End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<EditableZone()> _
Public Class VMNumberZone
    Inherits ViewModelBase

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Sub New()
        AddHandler Model.Instance.Resfreshed, _
            Sub(s As Object, a As EventArgs)
                OnPropertyChanged("FormatedNumber")
            End Sub

    End Sub
    Private _ValueSource As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    <EditableProperty()> _
    Public Property ValueSource As String
        Get
            Return _ValueSource
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("ValueSource")
            _ValueSource = value
            OnPropertyChanged("ValueSource")
        End Set
    End Property

    Private _Value As Double = Math.PI / 100
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    <EditableProperty(Group:="Data")> _
    Public Property Value As Double
        Get
            If Indicator IsNot Nothing And Indicator IsNot IndicatorKey.None Then
                Dim n = TryCast(Model.Instance.GetIndicator(Indicator.name), NumberIndicator)
                If n IsNot Nothing Then
                    Return n.Value
                End If
            End If
            Return _Value
        End Get
        Set(ByVal value As Double)
            OnPropertyChanging("Value")
            _Value = value
            OnPropertyChanged("Value")
            OnPropertyChanged("FormatedNumber")
        End Set
    End Property

    Private _Format As String = "0.00 %"
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    <EditableProperty(Group:="Data")> _
    Public Property Format As String
        Get
            Return _Format
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Format")
            _Format = value
            OnPropertyChanged("Format")
            OnPropertyChanged("FormatedNumber")
        End Set
    End Property

    Private _FormatPredefini As PredefinedFormat = PredefinedFormat.None
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    <EditableProperty(Group:="Data")> _
    Public Property FormatPredefini As PredefinedFormat
        Get
            Return _FormatPredefini
        End Get
        Set(ByVal value As PredefinedFormat)
            OnPropertyChanging("FormatPredefini")
            _FormatPredefini = value
            OnPropertyChanged("FormatPredefini")
        End Set
    End Property

    Private _FontSize As Double = 12
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property FontSize As Double
        Get
            Return _FontSize
        End Get
        Set(ByVal value As Double)
            OnPropertyChanging("FontSize")
            _FontSize = value
            OnPropertyChanged("FontSize")
        End Set
    End Property

    Private _FontBrush As Object
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property FontBrush As Object
        Get
            Return _FontBrush
        End Get
        Set(ByVal value As Object)
            OnPropertyChanging("FontBrush")
            _FontBrush = value
            OnPropertyChanged("FontBrush")
        End Set
    End Property

    Private _Indicator As IndicatorKey
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    <EditableProperty(Group:="Data")> _
    Public Property Indicator As IndicatorKey
        Get
            Return _Indicator
        End Get
        Set(ByVal value As IndicatorKey)
            OnPropertyChanging("Indicator")
            _Indicator = value
            OnPropertyChanged("Indicator")
        End Set
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property FormatedNumber As String
        Get
            Return Value.ToString(Format)
        End Get
    End Property

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<EditableZone()> _
Public Class VMGaugeZone
    Inherits ViewModelBase

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<EditableZone(), Serializable()> _
Public Class VMPieZone
    Inherits VMChartZone

    Private _MarkerPolice As Police = Indicateurs.Police.Default
    <EditableProperty(Group:="Marqueur")> _
    Public Property MarkerPolice As Police
        Get
            Return _MarkerPolice
        End Get
        Set(ByVal value As Police)
            OnPropertyChanging("MarkerPolice")
            _MarkerPolice = value
            OnPropertyChanged("MarkerPolice")
        End Set
    End Property

    Private _MarkerColor As Media.Color
    <EditableProperty(Group:="Marqueur")> _
    Public Property MarkerColor As Media.Color
        Get
            Return _MarkerColor
        End Get
        Set(ByVal value As Media.Color)
            OnPropertyChanging("TitleColor")
            _MarkerColor = value
            OnPropertyChanged("TitleColor")
        End Set
    End Property

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<EditableZone(), Serializable()> _
Public Class VMSplineZone
    Inherits VMChartZone


End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<EditableZone()> _
Public Class VMLineZone
    Inherits VMChartZone


End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<EditableZone(), Serializable()> _
Public Class VMBarZone
    Inherits VMChartZone

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<EditableZone(), Serializable()> _
Public Class VMHistogramZone
    Inherits VMChartZone


    Private _Brother As Object
    Public Property Brother As Object
        Get
            Return _Brother
        End Get
        Set(ByVal value As Object)
            OnPropertyChanging("Brother")
            _Brother = value
            OnPropertyChanged("Brother")
        End Set
    End Property

End Class

<EditableZone(), Serializable()> _
Public Class VMHistogramZoneSimple
    Inherits VMChartZone


End Class

<EditableZone(), Serializable()> _
Public Class VMFormatedTextZone
    Inherits ViewModelBase

End Class
Public Class VMImageZone
    Inherits ViewModelBase

End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class TestData
    Private Shared rnd As New Random(Now.Millisecond)

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property DataSpline As IEnumerable
        Get
            Return From e In Enumerable.Range(0, rnd.Next(10, 20))
                   Select e - rnd.NextDouble() + 0.5
        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property DataPie As IEnumerable
        Get
            Return From e In Enumerable.Range(0, 3)
                   Select
                     X = "Zone " & e,
                     Y = rnd.NextDouble()
        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property DataIndicator As DataView
        Get
            If data Is Nothing Then
                CreateData()
            End If
            Return data.DefaultView
        End Get
    End Property

    Friend Shared data As DataTable
    Private Shared _ModelVariables() As DataColumn

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property ModelVariables As IEnumerable
        Get
            Try
                Dim tbl = ModuleIndicateurs.BddInstance.ListeTraitements.TraitTbleEncours

                Return From e In tbl.ListeChampsSortie
                       Select Caption = e.Key, Column = e


            Catch ex As Exception

            End Try
        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Shared Sub CreateData()
        data = New DataTable()
        ReDim _ModelVariables(8)
        For i As Integer = 0 To 4
            _ModelVariables(i) = data.Columns.Add("TestCol_" & i, GetType(Double))
        Next
        _ModelVariables(5) = data.Columns.Add("Tiny", GetType(Double))
        _ModelVariables(6) = data.Columns.Add("Big", GetType(Double))
        _ModelVariables(7) = data.Columns.Add("Weight", GetType(Double))
        _ModelVariables(8) = data.Columns.Add("Id", GetType(String))

        For i As Integer = 0 To 2
            Dim r = data.NewRow()
            r("Id") = "Zone_" & (i + 1)
            r("Tiny") = 0.1 + 0.8 * rnd.NextDouble() * rnd.NextDouble()
            r("Big") = 1.0 + (3.0 - i) * rnd.NextDouble()
            r("Weight") = (i + 1.0) / 6.0
            data.Rows.Add(r)
        Next
    End Sub


End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class TestDataRow
    Implements IEnumerable

    Private data As New Hashtable
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Sub New(ByVal ParamArray props() As String)
        For Each p In props
            data.Add(p, Nothing)
        Next
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Default Public Property Item(ByVal prop As String) As Object
        Get
            Return data(prop)
        End Get
        Set(ByVal value As Object)
            data(prop) = value
        End Set
    End Property
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return data.Values
    End Function


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ref()
        Dim tbl = ModuleIndicateurs.BddInstance.ListeTraitements.TraitTbleEncours
        Dim nomtrait = "ohgod"
        Dim req = New RequeteBDD(nomtrait, _
                        New Cla_NomFichier(PTIC_Util.csteEmplacementCalculsInt & nomtrait & ".sas7bdat"), _
                        PTIC_Util.csteLibSASCalculsInt, _
                        ModuleIndicateurs.BddInstance.Connection.Connection)


        '  req.
    End Sub
End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Enum PredefinedFormat

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    None

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Percentage

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Number

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    [Date]

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    [Time]
End Enum


Public Class VMDataModel
    Inherits VMSectionBase


    Public Tables As New ObservableCollection(Of VMDataModelTable)

    Public Relations As New ObservableCollection(Of VMDataModelRelation)

    Public FactoryPattern As New ObservableCollection(Of VMDataModelFactoryPattern)
    Dim _qs As String

    Public Property QueryString As String
        Get
            Return _qs
        End Get
        Set(ByVal value As String)
            _qs = value
            OnPropertyChanged("QueryString")
        End Set
    End Property
End Class

Public Class VMDataModelTable
    Inherits ViewModelBase

    Public Columns As New ObservableCollection(Of VMDataModelColumn)

    Private _Name As String
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Name")
            _Name = value
            OnPropertyChanged("Name")
        End Set
    End Property


End Class

Public Class VMDataModelColumn
    Inherits ViewModelBase

    Private _Name As String
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Name")
            _Name = value
            OnPropertyChanged("Name")
        End Set
    End Property

    Private _Type As String
    Public Property Type As String
        Get
            Return _Type
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Type")
            _Type = value
            OnPropertyChanged("Type")
        End Set
    End Property

    Private _Types As IEnumerable
    Public ReadOnly Property Types As IEnumerable
        Get
            Return _Types
        End Get
    End Property

End Class

Public Class VMDataModelRelation
    Inherits ViewModelBase


End Class

Public Class VMDataModelFactoryPattern
    Inherits ViewModelBase

End Class

Public Class ZoneRichTextBox
    Inherits RichTextBox




    Public Property SelectionBackground As Media.Color
        Get
            Return GetValue(SelectionBackgroundProperty)
        End Get

        Set(ByVal value As Media.Color)
            SetValue(SelectionBackgroundProperty, value)
        End Set
    End Property


    Public Shared ReadOnly SelectionBackgroundProperty As DependencyProperty = _
                           DependencyProperty.Register("SelectionBackground", _
                           GetType(Media.Color), GetType(ZoneRichTextBox), _
                           New FrameworkPropertyMetadata(Media.Colors.Transparent, AddressOf SelectionPropertyChanged))



    Public Property SelectionForeground As Media.Color
        Get
            Return GetValue(SelectionForegroundProperty)
        End Get

        Set(ByVal value As Media.Color)
            SetValue(SelectionForegroundProperty, value)
        End Set
    End Property


    Public Shared ReadOnly SelectionForegroundProperty As DependencyProperty = _
                           DependencyProperty.Register("SelectionForeground", _
                           GetType(Media.Color), GetType(ZoneRichTextBox), _
                           New FrameworkPropertyMetadata(Media.Colors.Transparent, AddressOf SelectionPropertyChanged))




    Public Property SelectionFont As Media.FontFamily
        Get
            Return GetValue(SelectionFontProperty)
        End Get

        Set(ByVal value As Media.FontFamily)
            SetValue(SelectionFontProperty, value)
        End Set
    End Property

    Public Shared ReadOnly SelectionFontProperty As DependencyProperty = _
                           DependencyProperty.Register("SelectionFont", _
                           GetType(Media.FontFamily), GetType(ZoneRichTextBox), _
                           New FrameworkPropertyMetadata(SystemFonts.CaptionFontFamily, AddressOf SelectionPropertyChanged))





    Private Shared Sub SelectionPropertyChanged(ByVal d As System.Windows.DependencyObject, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
        Dim z = TryCast(d, ZoneRichTextBox)
        If z Is Nothing Then Return

        z.Selection.ApplyPropertyValue( _
            Documents.TextElement.ForegroundProperty, _
            New Media.SolidColorBrush(z.SelectionForeground))

        z.Selection.ApplyPropertyValue( _
          Documents.TextElement.BackgroundProperty, _
          New Media.SolidColorBrush(z.SelectionBackground))

        z.Selection.ApplyPropertyValue( _
            Documents.TextElement.FontFamilyProperty, _
            z.SelectionFont)

    End Sub

End Class

Public Class DynamicGrid
    Inherits Grid


    Public Property Rows As Integer
        Get
            Return GetValue(RowsProperty)
        End Get

        Set(ByVal value As Integer)
            SetValue(RowsProperty, value)
        End Set
    End Property

    Public Shared ReadOnly RowsProperty As DependencyProperty = _
                           DependencyProperty.Register("Rows", _
                           GetType(Integer), GetType(DynamicGrid), _
                           New FrameworkPropertyMetadata(1, AddressOf RowsOrColumnsChanged))

    Public Property Columns As Integer
        Get
            Return GetValue(ColumnsProperty)
        End Get

        Set(ByVal value As Integer)
            SetValue(ColumnsProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ColumnsProperty As DependencyProperty = _
                           DependencyProperty.Register("Columns", _
                           GetType(Integer), GetType(DynamicGrid), _
                           New FrameworkPropertyMetadata(1, AddressOf RowsOrColumnsChanged))


    Public Property SplitterStyle As Style
        Get
            Return GetValue(SplitterStyleProperty)
        End Get

        Set(ByVal value As Style)
            SetValue(SplitterStyleProperty, value)
        End Set
    End Property

    Public Shared ReadOnly SplitterStyleProperty As DependencyProperty = _
                           DependencyProperty.Register("SplitterStyle", _
                           GetType(Style), GetType(DynamicGrid), _
                           New FrameworkPropertyMetadata(Nothing, AddressOf SplitterStyleChanged))


    Public Property HorizontalSplitterStyle As Style
        Get
            Return GetValue(HorizontalSplitterStyleProperty)
        End Get

        Set(ByVal value As Style)
            SetValue(HorizontalSplitterStyleProperty, value)
        End Set
    End Property

    Public Shared ReadOnly HorizontalSplitterStyleProperty As DependencyProperty = _
                           DependencyProperty.Register("HorizontalSplitterStyle", _
                           GetType(Style), GetType(DynamicGrid), _
                           New FrameworkPropertyMetadata(Nothing, AddressOf VorHSplitterChanged))



    Public Property VerticalSplitterStyle As Style
        Get
            Return GetValue(VerticalSplitterStyleProperty)
        End Get

        Set(ByVal value As Style)
            SetValue(VerticalSplitterStyleProperty, value)
        End Set
    End Property

    Public Shared ReadOnly VerticalSplitterStyleProperty As DependencyProperty = _
                           DependencyProperty.Register("VerticalSplitterStyle", _
                           GetType(Style), GetType(DynamicGrid), _
                           New FrameworkPropertyMetadata(Nothing, AddressOf VorHSplitterChanged))


    Public Property SplitterEnabled As Boolean
        Get
            Return GetValue(SplitterEnabledProperty)
        End Get

        Set(ByVal value As Boolean)
            SetValue(SplitterEnabledProperty, value)
        End Set
    End Property

    Public Shared ReadOnly SplitterEnabledProperty As DependencyProperty = _
                           DependencyProperty.Register("SplitterEnabled", _
                           GetType(Boolean), GetType(DynamicGrid), _
                           New FrameworkPropertyMetadata(True))


    Private Shared Sub RowsOrColumnsChanged(ByVal d As System.Windows.DependencyObject, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
        Dim z = TryCast(d, DynamicGrid)
        If z Is Nothing Then Return

        If z.Columns <> z.ColumnDefinitions.Count _
             Or _
               z.Rows <> z.RowDefinitions.Count Then

            If z.Columns <> z.ColumnDefinitions.Count Then

                If z.Columns < z.ColumnDefinitions.Count Then
                    z.ColumnDefinitions.RemoveRange(z.Columns, z.ColumnDefinitions.Count - z.Columns)
                End If


                If z.Columns > z.ColumnDefinitions.Count Then
                    Dim oldcount As Integer = z.ColumnDefinitions.Count
                    For i As Integer = 0 To z.Columns - z.ColumnDefinitions.Count - 1
                        z.ColumnDefinitions.Add(New ColumnDefinition())
                    Next

                    For i As Integer = If(oldcount = 0, 1, oldcount) To Math.Max(z.Columns - 1, 1)
                        Dim gs = New GridSplitter()

                        Dim stylebinding As New Data.Binding("VerticalSplitterStyle")
                        stylebinding.Source = z
                        Dim rowspanbinding As New Data.Binding("Rows")
                        rowspanbinding.Source = z

                        Data.BindingOperations.SetBinding(gs, FrameworkElement.StyleProperty, stylebinding)
                        Data.BindingOperations.SetBinding(gs, Grid.RowSpanProperty, rowspanbinding)
                        gs.VerticalAlignment = Windows.VerticalAlignment.Stretch
                        gs.HorizontalAlignment = Windows.HorizontalAlignment.Left

                        ' z.Children.Add(gs)
                        Grid.SetColumn(gs, i)
                    Next
                End If
            End If

            If z.Rows <> z.RowDefinitions.Count Then

                If z.Rows < z.RowDefinitions.Count Then
                    z.RowDefinitions.RemoveRange(z.Rows, z.RowDefinitions.Count - z.Rows)
                End If

                If z.Rows > z.RowDefinitions.Count Then
                    Dim oldcount As Integer = z.RowDefinitions.Count
                    For i As Integer = 0 To z.Rows - z.RowDefinitions.Count - 1
                        z.RowDefinitions.Add(New RowDefinition())
                    Next

                    For i As Integer = If(oldcount = 0, 1, oldcount) To Math.Max(z.Rows - 1, 1)
                        Dim gs = New GridSplitter()

                        Dim stylebinding As New Data.Binding("HorizontalSplitterStyle")
                        stylebinding.Source = z
                        Dim columnspanbinding As New Data.Binding("Columns")
                        columnspanbinding.Source = z

                        Data.BindingOperations.SetBinding(gs, FrameworkElement.StyleProperty, stylebinding)
                        Data.BindingOperations.SetBinding(gs, Grid.ColumnSpanProperty, columnspanbinding)
                        gs.VerticalAlignment = Windows.VerticalAlignment.Top
                        gs.HorizontalAlignment = Windows.HorizontalAlignment.Stretch

                        '  z.Children.Add(gs)
                        Grid.SetRow(gs, i)
                    Next
                End If
            End If
        End If

    End Sub

    Private Shared Sub VorHSplitterChanged(ByVal d As System.Windows.DependencyObject, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
        Dim z = TryCast(d, DynamicGrid)
        If z Is Nothing Then Return

        Dim q = TryCast(e.NewValue, Style)
        If q Is Nothing Then Return

        q.BasedOn = z.SplitterStyle
    End Sub
    Private Shared Sub SplitterStyleChanged(ByVal d As System.Windows.DependencyObject, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
        Dim z = TryCast(d, DynamicGrid)
        If z Is Nothing Then Return

        Dim q = TryCast(e.NewValue, Style)

        If z.HorizontalSplitterStyle IsNot Nothing Then
            z.HorizontalSplitterStyle.BasedOn = q
        End If
        If z.VerticalSplitterStyle IsNot Nothing Then
            z.VerticalSplitterStyle.BasedOn = q
        End If
    End Sub
End Class

Public Class CustomChart
    Inherits XamChart



    Public Property BoundableSeries As IEnumerable
        Get
            Return GetValue(BoundableSeriesProperty)
        End Get

        Set(ByVal value As IEnumerable)
            SetValue(BoundableSeriesProperty, value)
        End Set
    End Property

    Public Shared ReadOnly BoundableSeriesProperty As DependencyProperty = _
                           DependencyProperty.Register("BoundableSeries", _
                           GetType(IEnumerable), GetType(CustomChart), _
                           New FrameworkPropertyMetadata(Nothing, AddressOf BoundableSeriesChanged))


    Private Shared Sub BoundableSeriesChanged(ByVal d As System.Windows.DependencyObject, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
        Dim z = TryCast(d, CustomChart)
        If z Is Nothing Then Return

        Dim nv = TryCast(e.NewValue, IEnumerable)

        If nv Is Nothing Then
            z.Series.Clear()
        Else
            z.Series.Clear()
            For Each elt In nv
                Dim ssr = New Series()
                elt.Extras.Brother = ssr
                ssr.DataSource = CType(elt, VMChartZoneSerie).data
                ssr.DataMapping = "Value=X;Label=Lbl"
                z.Series.Add(ssr)
            Next

            Dim cz = TryCast(z.DataContext, VMChartZone)


            If cz IsNot Nothing Then

                Dim czSeriesCollectionChanged As NotifyCollectionChangedEventHandler = _
                    Sub(sender As Object, elt As NotifyCollectionChangedEventArgs)
                        Select Case elt.Action
                            Case NotifyCollectionChangedAction.Add
                                Dim sr = New Series() 'With {.Style = z.Resources("StyleSerie"), .DataSource = elt.NewItems(0)}

                                sr.ChartType = ChartType.Column
                                z.Series.Add(sr)
                                Dim chser As VMChartZoneSerie = elt.NewItems(0)
                                chser.Extras.Brother = sr

                                Dim chserdatachanged = Sub(sender2 As Object, elt2 As EventArgs)
                                                           sr.DataSource = chser.data
                                                           sr.DataMapping = "Value=X;Label=Lbl"
                                                       End Sub

                                AddHandler chser.DataChanged, chserdatachanged
                                '  If z.Series.Count = 0 Then z.Series.Clear()

                                sr.DataSource = chser.data

                            Case NotifyCollectionChangedAction.Remove
                                Dim chser As VMChartZoneSerie = elt.OldItems(0)
                                z.Series.Remove(chser.Extras.Brother)
                        End Select
                    End Sub


                AddHandler cz.Series.CollectionChanged, czSeriesCollectionChanged
            End If
        End If
    End Sub



End Class

Public Class CustomSerie
    Inherits Series



    Public Property Descriptor As Object
        Get
            Return GetValue(DescriptorProperty)
        End Get

        Set(ByVal value As Object)
            SetValue(DescriptorProperty, value)
        End Set
    End Property

    Public Shared ReadOnly DescriptorProperty As DependencyProperty = _
                           DependencyProperty.Register("Descriptor", _
                           GetType(Object), GetType(CustomSerie), _
                           New FrameworkPropertyMetadata(Nothing))




End Class